<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="padding: 10px 0">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addRole()"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="roleList">
              <el-table-column type="index" label="序号" width="120" />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template #default="scope">
                  <el-button size="small" type="success" @click="setPermission(scope.row)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(scope.row)">编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="delRole(scope.row)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                :total="total"
                :current-page="pages.page"
                :page-size="pages.pagesize"
                layout="prev,pager,next"
                @current-change="changePage"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 弹出层 -->
    <el-dialog
      :title="roleForm.id?'编辑角色':'添加角色'"
      :visible.sync="showDialog"
      width="30%"
      @closed="btnCancel"
    >
      <el-form
        ref="form"
        :rules="rules"
        :model="roleForm"
        label-width="80px"
        size="normal"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <el-row type="flex" justify="cneter">
        <el-col :span="6"></el-col>
        <el-button size="small" @click="btnCancel">取消</el-button>
        <el-button size="small" type="primary" @click="btnOK">{{ roleForm.id?'更改':'添加' }}</el-button>
      </el-row>
    </el-dialog>

    <!-- 权限控制弹出层 -->
    <PermissionContrail ref="Permission" :show-permission-dialog.sync="showPermissionDialog" />
  </div>
</template>

<script>
import { getAllRoleAPI, delRoleAPI, addRoleAPI, getRoleDetailAPI, updateRoleDetailAPI } from '@/api/setting'
import PermissionContrail from '@/views/setting/components/PermissionContrail'
export default {
  components: {
    PermissionContrail
  },
  data () {
    return {
      roleID: '', // 角色的id
      showPermissionDialog: false, // 控制权限管理弹出层的显示和隐藏
      roleList: [],
      pages: {
        page: 1, // 页码
        pagesize: 10// 每页条数
      },
      total: 0, // 数据总数
      showDialog: false, // 控制弹出层显示和隐藏
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入成员名称', trigger: ['blur', 'change'] },
          { min: 2, max: 10, message: '请输入1-10位字符', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  created () {
    this.getAllRole()
  },
  methods: {
    // 分配权限处理
    async setPermission(row) {
      this.showPermissionDialog = true
      console.log('点击的每一个角色的id' + row.id)
      this.roleID = row.id // 角色的id
      // 通过ref调用子组件的方法把id值传给子组件的方法
      this.$refs.Permission.getRoleDetail(this.roleID)
    },
    // 调用获取所有角色列表的API
    async getAllRole () {
      const res = await getAllRoleAPI(this.pages)
      console.log(res)
      this.roleList = res.rows
      this.total = res.total
      // console.log(res)
    },
    // 点击删除按钮删除当前行数据
    async delRole (row) {
      console.log(row.id)
      // this.$confirm('此操作将永久删除该文件, 是否继续?', '提示').then(() => {
      //   // 调用删除接口
      //   delRoleAPI(row.id).then(res => {
      //     this.$message({
      //       type: 'success',
      //       message: '删除成功!'
      //     })
      //     this.getAllRole()
      //   })
      // }).catch()
      try {
        await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示')
        await delRoleAPI(row.id)
        this.getAllRole()
        this.$message.success('删除角色成功')
      } catch (error) {
        console.log(error)
      }
    },
    // 点击新增角色弹出弹出层
    addRole () {
      this.showDialog = true
    },
    // 点击弹出层的确认,调用接口储存数据
    btnOK () {
      this.$refs.form.validate(async(isOK) => {
        if (isOK) {
        // 判断是新增还是编辑
          if (this.roleForm.id) { // 有id就是编辑状态
            await updateRoleDetailAPI(this.roleForm)
          } else {
            await addRoleAPI(this.roleForm)
          }

          // console.log(res)
          this.getAllRole()
          this.$message.success('操作成功')
          this.showDialog = false
        }
      })
    },
    // 切换分页
    changePage(page) {
      console.log(page)
      this.pages.page = page
      this.getAllRole()
    },
    // 点击取消按钮,点击空白区域或者点击右上角x号,执行的事件
    btnCancel() {
      // 清空数据
      this.roleForm = {
        name: '',
        description: ''
      }
      // 移除校验
      this.$refs.form.resetFields()
      // 关闭弹窗
      this.showDialog = false
    },
    // 点击编辑按钮,调用接口获取最新的数据
    // 然后可以把用户最新输入的数据在发送给后台
    async editRole(data) {
      this.showDialog = true
      // 获取到用户最新的数据
      const res = await getRoleDetailAPI(data.id)
      this.roleForm = res
      // 把用户修改的值提交给服务器
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
